package com.ohuang.mylibrary;

import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.baseapp.utils.ScreenUtils;
import com.ohuang.baseapp.view.player.HmPlayer;
import com.ohuang.baseapp.view.player.api.ImplPlayer;
import com.ohuang.baseapp.view.player.constant.Constants;
import com.ohuang.baseapp.view.player.view.PlayerLoading;
import com.ohuang.baseapp.view.player.view.PlayerView;
import com.ohuang.baseapp.view.player.view.SimplePlayerController;
import com.ohuang.router_compiler.Router;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.bundle.AbilityInfo;

@Router(path = "lib/main2")
public class MainAbility2Slice extends AbilitySlice {
    private PlayerView playerView;
    private PlayerLoading loadingView;
    private SimplePlayerController controllerView;
    private String url = "entry/resources/base/media/gubeishuizhen.mp4";
    private ImplPlayer player;
    private ComponentContainer layout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main2);
        player = new HmPlayer.Builder(getContext()).setFilePath(url).create();
        player.getLifecycle().onStart();
        layout = (ComponentContainer) findComponentById(ResourceTable.Id_layout);
        playerView = (PlayerView) findComponentById(ResourceTable.Id_player_view);
        loadingView = (PlayerLoading) findComponentById(ResourceTable.Id_loading_view);
        controllerView = (SimplePlayerController) findComponentById(ResourceTable.Id_controller_view);
        playerView.bind(player);
        loadingView.bind(player);
        controllerView.bind(player);


    }

    @Override
    public void onActive() {
        super.onActive();
        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(() -> player.play(), Constants.NUMBER_1000);
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        int screenWidth = ScreenUtils.getScreenWidth(this);
        layout.setWidth(screenWidth);
        player.openGesture(displayOrientation == AbilityInfo.DisplayOrientation.LANDSCAPE);
    }


    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        player.getLifecycle().onForeground();
        HiLogUtil.error("Main2", "onForeground");
    }

    @Override
    protected void onBackground() {

        super.onBackground();
        player.getLifecycle().onBackground();
        HiLogUtil.error("Main2", "onBackground");
    }

    @Override
    protected void onStop() {
        loadingView.unbind();
        controllerView.unbind();
        playerView.unbind();
        player.getLifecycle().onStop();
        HiLogUtil.error("Main2", "onStop");
        super.onStop();
    }
}
